<template>
  <div class="reg">

    <div class="reg-box">
      <!-- 标题 -->
      <div class="title">后台管理系统</div>

      <!-- 表单 -->
      <el-form ref="regRef" :model="regForm" :rules="regRules">

        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="regForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="regForm.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item prop="repassword">
          <el-input v-model="regForm.repassword" placeholder="请确认密码" show-password></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" class="regBtn" @click="registerFun">注册</el-button>
          <el-link type="info" class="regLink" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { regAPI } from '@/api/index.js'
export default {
  data () {
    const samePwd = (rule, value, callback) => {
      if (this.regForm.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      // 注册表单数据
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      // 注册表单验证规则
      regRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10的大小写字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          }
        ],
        repassword: [
          {
            required: true,
            message: '请再次输入密码',
            trigger: 'blur'
          },
          {
            validator: samePwd,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    registerFun () {
      this.$refs.regRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error('请填写正确的注册信息')
        } else {
          const { data: res } = await regAPI(this.regForm)
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success('注册成功')
          this.$router.push('/login')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.reg {
  position: relative;
  height: 100vh;
  width: 100%;
  background-image: url(../../assets/backgroundImg.jpg);
  background-size: cover;
}

.reg-box {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  width: 400px;
  height: 335px;
  background-color: #fff;

  .title {
    text-align: center;
    font-size: 28px;
    color: #007AFF;
    margin-top: 10px;
  }

  .regBtn {
    width: 100%;
  }

  .regLink {
    line-height: 20px !important;
    margin-top: 10px;
  }
}
</style>
